// MegaDrawerPrefs.cpp : implementation file
//

#include "stdafx.h"
#include "resource.h"
#include "MegaDrawerPrefs.h"
#include "../GUI/Messages.h"
#include "../Capture/CapLib.h"
#include "../Lib/RMSHostEnt.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern BYTE * pSleepBitmapDataCompressed;
extern BYTE * pSleepBitmapData;
extern int sizeSleepCompressed;

#define IDC_PREF_IP			100
#define IDC_PREF_PORT		101
#define IDC_PREF_PASS		102
#define IDC_PREF_BITRATE	103
#define IDC_PREF_CLEARPIC	104
#define IDC_PREF_SETPIC		105
#define IDC_PREF_SOURCE		106
#define IDC_PREF_FPS		107
#define IDC_PREF_SIZE		108
#define IDC_PREF_CANCEL		109
#define IDC_PREF_OK			110
#define IDC_PREF_HOME		111


#define ENABLE_640x480
/////////////////////////////////////////////////////////////////////////////
// CMegaDrawerPrefs
CMegaDrawerPrefs::CMegaDrawerPrefs()
{
	m_pHost = NULL;
	m_nPort = 0;
	m_nCycle = 0;
}

CMegaDrawerPrefs::~CMegaDrawerPrefs()
{
}

void CMegaDrawerPrefs::SetBitmaps(UINT uiSkinSingle, UINT uiSkinDouble, UINT uiSkinSingleUn, UINT uiSkinDoubleUn)
{
	CMegaDrawer::SetBitmaps(uiSkinSingle, uiSkinDouble, uiSkinSingleUn, uiSkinDoubleUn);

	// create controls
	CRect rc(115, 115, 120, 120);
	m_StreamboxPower.LoadBitmap(IDB_STREAMBOX_POWER);
	DWORD dwStyle = WS_CHILD|WS_TABSTOP|ES_LEFT|ES_AUTOHSCROLL;

#if defined(_OEM_) && defined(_TEC_)
	m_Home.Create		(dwStyle, rc, this, IDC_PREF_HOME);
#endif //defined(_OEM_) && defined(_TEC_)
	m_IPCombo.Create	(WS_CHILD|WS_VSCROLL|CBS_DROPDOWNLIST|CBS_HASSTRINGS|CBS_OWNERDRAWFIXED, rc, this, IDC_PREF_IP);
	m_Port.Create		(dwStyle|ES_NUMBER, rc, this, IDC_PREF_PORT);
	m_Pass.Create		(dwStyle|ES_PASSWORD, rc, this, IDC_PREF_PASS);
	m_Bitrate.Create	(dwStyle|ES_NUMBER, rc, this, IDC_PREF_BITRATE);

	m_FPSCombo.Create	(WS_CHILD|WS_VSCROLL|CBS_DROPDOWNLIST|CBS_HASSTRINGS|CBS_OWNERDRAWFIXED, rc, this, IDC_PREF_FPS);
	// fill FPS combo
	char buf[10];
	for(int i = 1; i <= 30; i++)
	{
		sprintf(buf, "%d", i);
		m_FPSCombo.AddString(buf);
	}
#ifdef ENABLE_640x480
	m_SizeCombo.Create	(WS_CHILD|WS_VSCROLL|CBS_DROPDOWNLIST|CBS_HASSTRINGS|CBS_OWNERDRAWFIXED, rc, this, IDC_PREF_SIZE);
	// fill size combo
	m_SizeCombo.AddString("320x240");
	m_SizeCombo.AddString("640x480");
#endif

	dwStyle = WS_CHILD|BS_PUSHBUTTON;
	m_ClearPicBtn.Create("", dwStyle, rc, this, IDC_PREF_CLEARPIC);
	m_SetPicBtn.Create("", dwStyle, rc, this, IDC_PREF_SETPIC);
	m_SourceBtn.Create("", dwStyle, rc, this, IDC_PREF_SOURCE);
	m_CancelBtn.Create("", dwStyle, rc, this, IDC_PREF_CANCEL);
	m_OkBtn.Create("", dwStyle, rc, this, IDC_PREF_OK);

	m_ClearPicBtn.SetBitmaps(IDB_CLEAR_OUT, IDB_CLEAR_DOWN, IDB_CLEAR_OVER);
	m_SetPicBtn.SetBitmaps(IDB_SET_OUT, IDB_SET_DOWN, IDB_SET_OVER);
	m_SourceBtn.SetBitmaps(IDB_SOURCE_OUT, IDB_SOURCE_DOWN, IDB_SOURCE_OVER);
	m_CancelBtn.SetBitmaps(IDB_CANCEL_OUT, IDB_CANCEL_DOWN, IDB_CANCEL_OVER);
	m_OkBtn.SetBitmaps(IDB_OK_OUT, IDB_OK_DOWN, IDB_OK_OVER);

	// after control's creation finished - repostiion them
	RepositionControls();

	// after controls positioned - set background bitmaps for them
	m_ClearPicBtn.SetBackgroundBmp(m_pSkinSingle);
	m_SetPicBtn.SetBackgroundBmp(m_pSkinSingle);
	m_SourceBtn.SetBackgroundBmp(m_pSkinSingle);
	m_CancelBtn.SetBackgroundBmp(m_pSkinSingle);
	m_OkBtn.SetBackgroundBmp(m_pSkinSingle);
}

void CMegaDrawerPrefs::RepositionControls()
{
	// reposition controls
	if(m_bActive)
	{
		if(m_nSize == 1)
		{
#if defined(_OEM_) && defined(_TEC_)
			m_Home.SetWindowPos(NULL,73, 7, 200, 19, SWP_NOZORDER|SWP_SHOWWINDOW);
			m_ClearPicBtn.SetWindowPos(NULL, 352, 4, 0, 0, SWP_NOZORDER|SWP_SHOWWINDOW|SWP_NOSIZE);
			m_SetPicBtn.SetWindowPos(NULL, 405, 4, 0, 0, SWP_NOZORDER|SWP_SHOWWINDOW|SWP_NOSIZE);

			m_IPCombo.SetWindowPos(NULL, 73, 40, 124, 100, SWP_NOZORDER|SWP_SHOWWINDOW);
			m_Port.SetWindowPos(NULL,	233, 43, 40, 19, SWP_NOZORDER|SWP_SHOWWINDOW);
			m_Pass.SetWindowPos(NULL,	376, 43, 80, 19, SWP_NOZORDER|SWP_SHOWWINDOW);

			m_Bitrate.SetWindowPos(NULL,140, 81, 40, 19, SWP_NOZORDER|SWP_SHOWWINDOW);
			m_FPSCombo.SetWindowPos(NULL, 261, 78, 40, 100, SWP_NOZORDER|SWP_SHOWWINDOW);
#ifdef ENABLE_640x480
			m_SizeCombo.SetWindowPos(NULL, 376, 78, 80, 100, SWP_NOZORDER|SWP_SHOWWINDOW);
#endif

			m_SourceBtn.SetWindowPos(NULL, 15, 118, 0, 0, SWP_NOZORDER|SWP_SHOWWINDOW|SWP_NOSIZE);
			m_CancelBtn.SetWindowPos(NULL, 346, 118, 0, 0, SWP_NOZORDER|SWP_SHOWWINDOW|SWP_NOSIZE);
			m_OkBtn.SetWindowPos(NULL, 406, 118, 0, 0, SWP_NOZORDER|SWP_SHOWWINDOW|SWP_NOSIZE);
#else //defined(_OEM_) && defined(_TEC_)
  			m_IPCombo.SetWindowPos(NULL,73, 5, 120, 100, SWP_NOZORDER|SWP_SHOWWINDOW);
  			m_Port.SetWindowPos(NULL,	227, 7, 40, 19, SWP_NOZORDER|SWP_SHOWWINDOW);
  			m_Pass.SetWindowPos(NULL,	68, 43, 70, 19, SWP_NOZORDER|SWP_SHOWWINDOW);
  			m_Bitrate.SetWindowPos(NULL,137, 81, 40, 19, SWP_NOZORDER|SWP_SHOWWINDOW);
   
  			m_ClearPicBtn.SetWindowPos(NULL, 287, 40, 0, 0, SWP_NOZORDER|SWP_SHOWWINDOW|SWP_NOSIZE);
  			m_SetPicBtn.SetWindowPos(NULL, 347, 40, 0, 0, SWP_NOZORDER|SWP_SHOWWINDOW|SWP_NOSIZE);
  			m_SourceBtn.SetWindowPos(NULL, 15, 118, 0, 0, SWP_NOZORDER|SWP_SHOWWINDOW|SWP_NOSIZE);

			m_FPSCombo.SetWindowPos(NULL, 260, 78, 40, 100, SWP_NOZORDER|SWP_SHOWWINDOW);
#ifdef ENABLE_640x480
  			m_SizeCombo.SetWindowPos(NULL, 358, 78, 80, 100, SWP_NOZORDER|SWP_SHOWWINDOW);
#endif

   			m_CancelBtn.SetWindowPos(NULL, 346, 118, 0, 0, SWP_NOZORDER|SWP_SHOWWINDOW|SWP_NOSIZE);
   			m_OkBtn.SetWindowPos(NULL, 406, 118, 0, 0, SWP_NOZORDER|SWP_SHOWWINDOW|SWP_NOSIZE);
#endif //defined(_OEM_) && defined(_TEC_)
		}
		else
		{
		}
	}
	else
	// if drawer unactive - hide all controls
	{
#if defined(_OEM_) && defined(_TEC_)
		m_Home.ShowWindow(SW_HIDE);
#endif //defined(_OEM_) && defined(_TEC_)
		m_ClearPicBtn.ShowWindow(SW_HIDE);
		m_SetPicBtn.ShowWindow(SW_HIDE);

		m_IPCombo.ShowWindow(SW_HIDE);
		m_Port.ShowWindow(SW_HIDE);
		m_Pass.ShowWindow(SW_HIDE);

		m_Bitrate.ShowWindow(SW_HIDE);
		m_FPSCombo.ShowWindow(SW_HIDE);

		m_SourceBtn.ShowWindow(SW_HIDE);
		m_CancelBtn.ShowWindow(SW_HIDE);
		m_OkBtn.ShowWindow(SW_HIDE);
	}
}

void CMegaDrawerPrefs::SetServerParams(RMSHostEnt * pHost, int nPort, int nCycle)
{
	m_pHost = pHost;
	m_nPort = nPort;
	m_nCycle = nCycle;

	// fill IP combobox
	IPArr arr;
	m_pHost->GetIPArr(arr);
	for(IPArr::const_iterator it = arr.begin(); it != arr.end(); it++)
	{
		String str = (*it).second;
		m_IPCombo.AddString(str.c_str());
	}
	m_IPCombo.RecalcMinWidth();

	String str;
	m_pHost->DefaultIPString(str);
	m_nIPNum = m_IPCombo.SelectString(-1, str.c_str());
	// fill port
	char buf[1024];
	m_Port.SetWindowText(itoa(m_nPort, buf, 10));
}

#if defined(_OEM_) && defined(_TEC_)
void CMegaDrawerPrefs::SetHomePath(CString home)
{
	m_homePath = home;
	m_Home.SetWindowText(m_homePath);
}
#endif //defined(_OEM_) && defined(_TEC_)

void CMegaDrawerPrefs::SetPassword(CString strPass)
{
	m_strPass = strPass;
	m_Pass.SetWindowText(m_strPass);
}

void CMegaDrawerPrefs::SetBitrate(int nBitrate)
{
	m_nBitrate = nBitrate;
	char buf[100];
	m_Bitrate.SetWindowText(itoa(m_nBitrate, buf, 10));
}

void CMegaDrawerPrefs::SetFPS(int nFPS)
{
	m_nFPS = nFPS;
	m_FPSCombo.SetCurSel(m_nFPS-1);
}

void CMegaDrawerPrefs::SetFrameSize(bool b640x480)
{
	m_b640x480 = b640x480;
#ifdef ENABLE_640x480
	if(m_b640x480)
		m_SizeCombo.SetCurSel(1);
	else
		m_SizeCombo.SetCurSel(0);
#endif
}

BEGIN_MESSAGE_MAP(CMegaDrawerPrefs, CMegaDrawer)
	//{{AFX_MSG_MAP(CMegaDrawerPrefs)
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	ON_WM_ERASEBKGND()

	ON_BN_CLICKED(IDC_PREF_CLEARPIC, OnClearPic)
	ON_BN_CLICKED(IDC_PREF_SETPIC, OnSetPic)
	ON_BN_CLICKED(IDC_PREF_SOURCE, OnSource)
	ON_CBN_SELENDOK(IDC_PREF_FPS, OnFPSChange)
	ON_CBN_SELENDOK(IDC_PREF_SIZE, OnSizeChange)
	ON_BN_CLICKED(IDC_PREF_CANCEL, OnCancel)
	ON_BN_CLICKED(IDC_PREF_OK, OnOk)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMegaDrawerPrefs message handlers
void CMegaDrawerPrefs::OnMouseMove(UINT nFlags, CPoint point) 
{
#ifdef _OEM_
	CSize sz = m_pBackgroundBmp->GetBitmapDimension();
	if(point.x >= 200 && point.x <= 320 && point.y > sz.cy-45)
		::SetCursor(::LoadCursor(::AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDC_HANDARROW)));
#endif

	CMegaDrawer::OnMouseMove(nFlags, point);
}

void CMegaDrawerPrefs::OnLButtonDown(UINT nFlags, CPoint point) 
{
#ifdef _OEM_
	CSize sz = m_pBackgroundBmp->GetBitmapDimension();
	if(point.x >= 200 && point.x <= 320 && point.y > sz.cy-45)
		::ShellExecute(GetSafeHwnd(), "open", "http:\\\\www.streambox.com", NULL, NULL, SW_SHOWDEFAULT);
#endif
	CMegaDrawer::OnLButtonDown(nFlags, point);
}

BOOL CMegaDrawerPrefs::OnEraseBkgnd(CDC* pDC) 
{
	// TODO: Add your message handler code here and/or call default
	BOOL bRes = CMegaDrawer::OnEraseBkgnd(pDC);

	// in OEM versions - draw "powered by Streambox"
#ifdef _OEM_
	CRect rc;
	GetClientRect(&rc);
	CSize sz = m_StreamboxPower.GetBitmapDimension();
	m_StreamboxPower.Draw(pDC, rc.right-sz.cx-12, rc.bottom-45-sz.cy+12);
#endif

	return bRes;
}

void CMegaDrawerPrefs::OnClearPic()
{
    if (pSleepBitmapDataCompressed)
        free(pSleepBitmapDataCompressed);

	pSleepBitmapDataCompressed=0;

    char filename[1000];
    char *fn2;
    ::GetModuleFileName(0, filename, 1000);

    fn2=strrchr(filename,'\\');
    fn2[0]=0;

    strcat(filename,"\\default.sleep");
    remove(filename);	
}

void CMegaDrawerPrefs::OnSetPic()
{
	CFileDialog dlg(1, 0, 0, OFN_HIDEREADONLY, "BMP Image|*.BMP|");
    if(IDOK == dlg.DoModal()) 
	{
		FILE *f;
		f = fopen((char*)(LPCSTR)dlg.GetPathName(),"rb");
		if (!f) return;

		int size=filelength(fileno(f));

		if (pSleepBitmapDataCompressed)
			free(pSleepBitmapDataCompressed);

		pSleepBitmapData = (BYTE*)malloc(size);

		fread(pSleepBitmapData,size,1,f);
		fclose(f);

		BYTE *tmp;
		int newsize;
		LPBITMAPINFOHEADER lp=(LPBITMAPINFOHEADER)(pSleepBitmapData+sizeof(BITMAPFILEHEADER));
    
		if ((lp->biWidth!=320) || (lp->biHeight!=240) ||(lp->biBitCount!=24)) 
		{
			MessageBox("320x240 24-bit RGB bitmap is required","Cannot set custom image",MB_OK);
			if (pSleepBitmapData)
				free(pSleepBitmapData);

			return ;
		}

		CompressStillImage(((BYTE*)lp)+(lp->biSize),(void**)&tmp,&newsize);
		pSleepBitmapDataCompressed = (BYTE*)malloc(newsize);
		memcpy(pSleepBitmapDataCompressed,tmp,newsize);
		sizeSleepCompressed = newsize;

		char filename[1000];
		char *fn2;
		GetModuleFileName(0,filename,1000);

		fn2=strrchr(filename,'\\');
		fn2[0]=0;

		strcat(filename,"\\default.sleep");

		f = fopen(filename,"wb");
		fwrite(tmp,newsize,1,f);
		fclose(f);
	}
}

void CMegaDrawerPrefs::OnSource()
{
	GetParent()->SendMessage(WM_SELECT_VIDEO_SOURCE);
}

void CMegaDrawerPrefs::OnFPSChange()
{
}

void CMegaDrawerPrefs::OnSizeChange()
{
}

void CMegaDrawerPrefs::OnCancel()
{
	char buf[1024];
	// restore old values
	m_IPCombo.SetCurSel(m_nIPNum);							// IP
	m_Port.SetWindowText(itoa(m_nPort, buf, 10));			// port

#if defined(_OEM_) && defined(_TEC_)
	m_Home.SetWindowText(m_homePath);						// home path
#endif //defined(_OEM_) && defined(_TEC_)
	m_Pass.SetWindowText(m_strPass);						// password
	m_Bitrate.SetWindowText(itoa(m_nBitrate, buf, 10));		// bitrate

	m_FPSCombo.SetCurSel(m_nFPS-1);							// fps
#ifdef ENABLE_640x480
	if(m_b640x480)
		m_SizeCombo.SetCurSel(1);
	else
		m_SizeCombo.SetCurSel(0);
#endif
}

void CMegaDrawerPrefs::OnOk()
{
	// check server params
	int nIP = m_IPCombo.GetCurSel();
	char buf[1024];
	m_Port.GetWindowText(buf, 1024);
	int nPort = atoi(buf);
	int nCycle = m_nCycle;

	// if some changed - send message to parent
	if(nIP != m_nIPNum || nPort != m_nPort)
	{
		WPARAM wParam = nIP;
		LPARAM lParam = nPort;
		GetParent()->SendMessage(WM_CHANGE_SERVER_PARAMS, wParam, lParam);
		m_nIPNum = nIP;
		m_nPort = nPort;
	}

	// check cyclic buffer size
	if(nCycle != m_nCycle)
	{
		GetParent()->SendMessage(WM_CHANGE_CYCLIC_BUFFER, nCycle);
		m_nCycle = nCycle;
	}

#if defined(_OEM_) && defined(_TEC_)
	// check home path
	m_Home.GetWindowText(buf, 1024);
	if(0 != m_homePath.Compare(buf))
	{
		GetParent()->SendMessage(WM_CHANGE_HOMEPATH, (WPARAM)buf);
		m_homePath = buf;
	}
#endif //defined(_OEM_) && defined(_TEC_)

	// check password
	m_Pass.GetWindowText(buf, 1024);
	if(0 != m_strPass.Compare(buf))
	{
		GetParent()->SendMessage(WM_CHANGE_PASSWORD, (WPARAM) buf);
		m_strPass = buf;
	}

	// check bitrate
	m_Bitrate.GetWindowText(buf, 1024);
	int nBitrate = atoi(buf);
	if(nBitrate != m_nBitrate)
	{
		GetParent()->SendMessage(WM_CHANGE_BITRATE, (WPARAM) nBitrate);
		m_nBitrate = nBitrate;
	}

	// check frame rate
	int nFPS = m_FPSCombo.GetCurSel()+1;
	if(nFPS != m_nFPS)
	{
		GetParent()->SendMessage(WM_CHANGE_FPS, (WPARAM) nFPS);
		m_nFPS = nFPS;
	}
#ifdef ENABLE_640x480
	bool b640x480 = (m_SizeCombo.GetCurSel() == 1);
	if(m_b640x480 != b640x480)
	{
		GetParent()->SendMessage(WM_CHANGE_SIZE, (WPARAM) b640x480);
		m_b640x480 = b640x480;
	}
#endif
}
